package com.yinshan.printer.service;

import android.os.IBinder;
import android.util.Log;

import com.szzt.sdk.device.Device;
import com.szzt.sdk.device.DeviceManager;
import com.szzt.sdk.device.aidl.IPrinterListener;
import com.szzt.sdk.device.printer.Printer;
import com.yinshan.printer.bean.PrintData;

import java.util.ArrayList;

/**
 * 证通POS机打印服务
 */
public class KSPrintService extends BaseIntentService {

    private DeviceManager deviceManager;
    private Printer printer;

    @Override
    void initPrint() {
        deviceManager = DeviceManager.getInstance();
        deviceManager.start(deviceManagerListener);
        deviceManager.getSystemManager();
    }

    @Override
    void print(ArrayList<PrintData> printDatas) {
        printer = getPrinter();
        if (null == printer) {
            Log.d(TAG, "printBill printer is null");
            return;
        }
        if (printer.open() < 0) {
            Log.d(TAG, "printer is open fail");
            return;
        }
        if (printer.getStatus() == Printer.STATUS_OK) {

            for (PrintData item : printDatas) {
                printer.addStr(item.getText(), getFont(item.getTextSize()), item.isBold(), getAlign(item.getTextAlign()));
            }
            printer.addFeedPaper(4, Printer.Unit.LINE);
            printer.start(new IPrinterListener() {
                @Override
                public void PrinterNotify(int i) {
                    if (i != 0) {
                        switch (printer.getStatus()) {
                            case Printer.STATUS_NO_PAPER:
                                showToast("打印缺纸");
                                break;
                            default:
                                Log.d(TAG, "printer.start:" + printer.getStatus());
                                break;
                        }
                    }
                }

                @Override
                public IBinder asBinder() {
                    return null;
                }
            });
        }
    }


    private int getFont(PrintData.Font myFont) {
        int font = Printer.Font.NORMAL;
        switch (myFont) {
            case BIG:
                font = Printer.Font.FONT_3;
                break;
            case NORMAL:
                font = Printer.Font.FONT_2;
                break;
            default:
                break;
        }
        return font;
    }

    private int getAlign(PrintData.Align myAlign) {
        int align = Printer.Align.CENTER;
        switch (myAlign) {
            case LEFT:
                align = Printer.Align.LEFT;
                break;
            case RIGHT:
                align = Printer.Align.RIGHT;
                break;
            default:
                break;
        }
        return align;
    }

    private DeviceManager.DeviceManagerListener deviceManagerListener = new DeviceManager.DeviceManagerListener() {
        @Override
        public int deviceEventNotify(Device device, int i) {
            return 0;
        }

        @Override
        public int serviceEventNotify(int event) {
            if (event == EVENT_SERVICE_CONNECTED) {
                Log.d(TAG, "打印机连接");
            } else if (event == EVENT_SERVICE_VERSION_NOT_COMPATABLE) {
                Log.d(TAG, "SDK Version is not compatable!!!");
            } else if (event == EVENT_SERVICE_DISCONNECTED) {
                deviceManager.start(deviceManagerListener);
                Log.d(TAG, "打印机断开连接");
            }
            return 0;
        }
    };

    public Printer getPrinter() {
        Log.i(TAG, "获取打印机");
        Device[] printers = deviceManager.getDeviceByType(Device.TYPE_PRINTER);
        if (printers != null) {
            Log.i(TAG, "打印机获取成功");
            return (Printer) printers[0];
        }
        Log.i(TAG, "打印机获取失败");
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        if (null != printer && printer.getStatus() != Device.STATUS_CLOSED) {
            printer.close();
            printer = null;
        }
        if (null != deviceManager) {
            deviceManager.stop();
            deviceManager = null;
        }
    }
}
